package com.ds.client.actors;

import java.util.Date;

import org.lwjgl.util.vector.Vector3f;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.ds.client.main.Main;

/**
 * Car actor class, maintains car informations
 */
public class Car extends Actor {
	private int id;
	public static final String[] CAR_PATH = { "res/Car1.pack", "res/Car2.pack",
			"res/Car3.pack", "res/Car4.pack" };
	public static final String CAR1 = "res/Car1.pack";
	public static final String CAR2 = "res/Car2.pack";
	public static final int START_X_CAR1 = 480;
	public static final int START_X_CAR2 = 570;
	private float speed = 1.0f;
	private float x;
	private float y;
	private int explodeTime;
	float stateTime;
	private Vector3f newPos = new Vector3f(0f, 0f, 0f);

	public Boolean moving = false;
	private int currentIndex;
	TextureAtlas atlas;
	TextureAtlas colAtlas;
	TextureAtlas tombAtlas;
	TextureRegion walksFrame;
	Animation animation;
	int tombShowTime = 0;
	boolean tombShow = false;
	int margin = 2;
	int pixHeight = 5;
	int hpWidth = 50;
	Pixmap pixmap;
	Texture pixTexture;
	TextureRegion pixRegion;
	Pixmap pixmapBase;
	Texture pixTextureBase;
	TextureRegion pixRegionBase;
	float maxHp = 5f;
	float currentHp = 5f;

	public float getCurrentHp() {
		return currentHp;
	}

	public void setCurrentHp(float currentHp) {
		this.currentHp = currentHp;
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	public Car(String path, int startX, String name) {
		super(name);
		atlas = new TextureAtlas(Gdx.files.internal(path));
		colAtlas = new TextureAtlas(Gdx.files.internal("res/bomb.pack"));
		tombAtlas = new TextureAtlas(Gdx.files.internal("res/tomb/tomb.pack"));

		this.height = 50f;
		this.width = 50f;
		x = startX;
		y = (Main.GameHight - this.height) / 2;
		setCurrentIndex(1);
		speed = 1.0f;
		this.moving = true;
		explodeTime = 1;
	}

	public Car(String path, int startX, String name, int id) {
		super(name);
		this.id = id;
		atlas = new TextureAtlas(Gdx.files.internal(path));
		colAtlas = new TextureAtlas(Gdx.files.internal("res/bomb.pack"));
		tombAtlas = new TextureAtlas(Gdx.files.internal("res/tomb/tomb.pack"));
		this.height = 50f;
		this.width = 50f;
		x = startX;
		y = (Main.GameHight - this.height) / 2;
		setCurrentIndex(1);
		speed = 1.0f;
		this.moving = true;
		explodeTime = 1;
	}

	/**
	 * draw the HP rectangle
	 */
	public void drawHpRectangle() {
		pixmap = new Pixmap(64, 8, Format.RGBA8888);
		pixmap.setColor(Color.BLACK);
		pixmap.drawRectangle(0, 0, hpWidth, pixHeight);
		pixmap.setColor(Color.GREEN);
		pixmap.fillRectangle(0, 1, (int) (hpWidth * currentHp / maxHp),
				pixHeight - 2);
		pixTexture = new Texture(pixmap);
		pixRegion = new TextureRegion(pixTexture, hpWidth, pixHeight);

		pixmapBase = new Pixmap(64, 8, Format.RGBA8888);
		pixmapBase.setColor(Color.BLACK);
		pixmapBase.drawRectangle(0, 0, hpWidth, pixHeight);
		pixmapBase.setColor(Color.RED);
		pixmapBase.fillRectangle(0, 1, (int) (hpWidth* maxHp), pixHeight - 2);
		pixTextureBase = new Texture(pixmapBase);
		pixRegionBase = new TextureRegion(pixTextureBase, hpWidth, pixHeight);

	}

	/**
	 * Car constructor with picture path
	 * @param path
	 */
	public Car(String path) {
		atlas = new TextureAtlas(Gdx.files.internal(path));
		colAtlas = new TextureAtlas(Gdx.files.internal("res/bomb.pack"));
		tombAtlas = new TextureAtlas(Gdx.files.internal("res/tomb/tomb.pack"));
		this.height = 50f;
		this.width = 50f;
		setCurrentIndex(1);
		speed = 1.0f;
		this.moving = true;
		explodeTime = 1;
	}

	public int getID() {
		return this.id;
	}

	public static Car getCar1(String name) {
		return new Car(CAR1, START_X_CAR1, "Car1");

	}

	public static Car getCar2(String name) {
		return new Car(CAR2, START_X_CAR2, "Car2");

	}

	/**
	 * check if the collision car show tomb
	 */
	public void checkTomb() {
		if (tombShowTime == 0) {
			tombShowTime = new Date().getSeconds();
		} else {
			if (new Date().getSeconds() - tombShowTime >= 2) {
				tombShow = true;
			}
		}
	}

	/**
	 * draw method, automatically invoked by stage
	 */
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		TextureRegion currentFrame;
		stateTime += Gdx.graphics.getDeltaTime();
		drawHpRectangle();
		
		// if the car still alive, change the car direction
		if (moving) {
			currentFrame = atlas.findRegion((int) this.currentIndex + "");
		}
		// if the car is dead
		else {
			checkTomb();
			// show ocllision pictures
			if (!tombShow) {
				showCollision();
				currentFrame = animation.getKeyFrame(stateTime, true);
			}
			// show tomb pictures
			else {
				currentFrame = tombAtlas.findRegion("grave30");
			}
		}
		// draw base hp rectangle
		batch.draw(pixRegionBase, this.getX(), this.getY() + 50);
		// draw base hp rectangle
		batch.draw(pixRegion, this.getX(), this.getY() + 50);
		batch.draw(currentFrame, this.x, this.y);

	}

	public void showCollision() {
		TextureRegion[] walksFrame;
		walksFrame = new TextureRegion[4];
		walksFrame[0] = colAtlas.findRegion("bomb0");
		walksFrame[1] = colAtlas.findRegion("bomb1");
		walksFrame[2] = colAtlas.findRegion("bomb2");
		walksFrame[3] = colAtlas.findRegion("bomb3");
		animation = new Animation(0.25f, walksFrame);
	}

	/**
	 * get a new position(for rmi use)
	 * 
	 * @param x
	 * @param y
	 */
	public void updatePos(Vector3f newPos) {
		setX(newPos.x);
		setY(newPos.y);
		currentIndex = (int) newPos.z;
	}

	@Override
	public Actor hit(float arg0, float arg1) {
		if (x > 0 && y > 0 && this.height > y && this.width > x) {
			this.x += 1;
			this.y += 1;
			this.parent.findActor("bg").x -= 1;
			return this;
		} else {
			return null;
		}
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public Vector3f getNewPos() {
		return newPos;
	}

	public void setNewPos(float x, float y, float z) {
		this.newPos = new Vector3f(x, y, z);
	}

	public int getExplodeTime() {
		return this.explodeTime;
	}

	public void setExplodeTime() {
		this.explodeTime -= 1;
	}

	public float getSpeed() {
		return this.speed;
	}

	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public static Car getCar(int carType) {
		return new Car(CAR_PATH[carType]);
	}
}
